package bookManage.model;

import java.io.Serializable;
import java.util.UUID;

import bookManage.util.CardUtil;

public class CardModel implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Integer id;// id
	private String cardId;// 借阅卡号
	private String name;// 姓名
	private String pass;// 密码

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		if (search(id, null)) {
			this.id = id;
			return;
		}
		System.out.print("借阅卡id重复，已为您自动生成一个id：");
		this.id = (int) (Math.random() * 2000000000 + 0);
		System.out.println(this.id);
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		if (search(null, cardId)) {
			this.cardId = cardId;
			return;
		}
		System.out.print("图书ISBN码重复，已为您自动生成一个ISBN码：");
		this.cardId = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
		System.out.println(this.cardId);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	/**
	 * 集合中是否有重复的id
	 * 
	 * @param id
	 * @param cardId
	 * @return
	 */
	public boolean search(Integer id, String cardId) {
		if (id != null) {
			for (CardModel card : CardUtil.cards) {
				if (card.getId().equals(id)) {
					return false;
				}
			}
		}

		if (cardId != null) {
			for (CardModel card : CardUtil.cards) {
				if (card.getCardId().equals(cardId)) {
					return false;// false 说明有重复
				}
			}
		}

		return true;// true 遍历一遍未找到重复的
	}

	@Override
	public String toString() {
		return "CardModel [id=" + id + ", cardId=" + cardId + ", name=" + name + ", pass=" + pass + "]";
	}
	
	
}
